################################################################################

# This is the script to create Figure 1. Conceptual illustration of crowd 
# classification problem for numeric estimates

################################################################################

###############
# Preparation #
###############

# Loading dependencies
library(tidyverse)

#############################################
# Creating an arbitrary belief distribution #
#############################################

# This is a totally arbitrary belief distribution (generated by a random 
# degroot process) chosen to illustrate key principles:
sample_data = data.frame(
  pre=c(6.3,7,8,9.2,12,13,16,18,20,23)
  , post=c(6.3,7,10.2,10.4,12,13,13.8,14.7,15.6,16.8)
  , group=1:10
) %>%
  mutate(
    initial=pre
  ) %>%
  gather(key="round", value="response", -c(group, initial)) %>%
  arrange(
    (round)
  ) %>%
  mutate(
    round=factor(round, levels=c("pre","post"))
  ) 

#######################
# Creating the figure #
#######################

# Creates the figure
ggplot(sample_data, aes(x=factor(round(initial)), y=response, group=group)) +
  geom_hline(yintercept=10, linetype="solid", color="#888888")+
  geom_hline(yintercept=mean(sample_data$initial), linetype="dashed", color="#888888")+
  geom_hline(yintercept=mean(sample_data$response[sample_data$round=="post"]), linetype="dotted", color="#222222")+
  geom_line() +
  geom_point(aes(size=round, color=round)) + 
  scale_color_manual(values=c("#8A8A8A","black")) +
  scale_size_manual(values=c(2,3))+
  #ylim(c(5,25))+
  #guides(color=F)+
  labs(x="", y="")+ 
  coord_flip()+
  theme_test()

# Saves the figure
ggsave("Figures/Figure 1.png", width=4, height=4)

###################################
# Getting values for illustration #
###################################

# Pre discussion mean numerical estimate
mean(sample_data$response[sample_data$round=="pre"])

# Post discussion mean numerical estimate
mean(sample_data$response[sample_data$round=="post"])

# Per discussion proportion of ppl who would vote against
mean(sample_data$response[sample_data$round=="pre"]>=10)

# Post discussion proportion of ppl who would vote against
mean(sample_data$response[sample_data$round=="post"]>=10)
